package com.wjy.icu.task;

import com.wjy.icu.domain.EquipmentFunc;
import com.wjy.icu.domain.EquipmentFuncExample;
import com.wjy.icu.mapper.EquipmentFuncMapper;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 雾化
 */
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class AtomizerStatusTask {

    @Resource
    EquipmentFuncMapper equipmentFuncMapper;

    //3.添加定时任务
    @Scheduled(cron = "0/2 * * * * ?")
    private void configureTasks() throws Exception{
        EquipmentFuncExample example = new EquipmentFuncExample();
        example.createCriteria().andAtomizerTreatmentStatusEqualTo(31).andAtomizerTreatmentEndLessThan(LocalDateTime.now());
        List<EquipmentFunc> equipmentFuncs = equipmentFuncMapper.selectByExample(example);
        equipmentFuncs.stream().forEach(equipmentFunc -> {
            equipmentFunc.setAtomizerTreatmentStatus(15);
            equipmentFuncMapper.updateByPrimaryKeySelective(equipmentFunc);
        });
    }
}
